package com.aps.algorithm.kpi.excutor;

import com.aps.algorithm.entity.ApsKpiTemplate;
import lombok.extern.slf4j.Slf4j;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Author nbZhuozh
 * @Date 2025-11-06  17:33
 *
 * 基于算法 排程数据 （工序级、设备级） 结合 行业模板计算 api指标
 **/
@Component
@Getter
@NoArgsConstructor
@Slf4j
public class KpiExcuteEngine<T> implements InitializingBean {

    private KpiTemplateExcutor<T> excutor;

    private T apsData;

    private List<ApsKpiTemplate> apsKpiTemplate;

    private Map<String, Object> kpiResultMap;

    public KpiExcuteEngine(KpiTemplateExcutor<T> excutor , T apsData) {
        this.excutor = excutor;
        this.apsData = apsData;
    }

    public KpiExcuteEngine(KpiTemplateExcutor<T> excutor , T apsData, List<ApsKpiTemplate> apsKpiTemplate) {
        this.excutor = excutor;
        this.apsData = apsData;
        this.apsKpiTemplate = apsKpiTemplate;
    }

    public  static <T> KpiExcuteEngine<T> build(KpiTemplateExcutor<T> excutor , T apsData){
        return new KpiExcuteEngine<>(excutor , apsData);
    }

    public  static <T> KpiExcuteEngine<T> build(KpiTemplateExcutor<T> excutor , T apsData, List<ApsKpiTemplate> apsKpiTemplate){
        return new KpiExcuteEngine<>(excutor , apsData, apsKpiTemplate);
    }


    public Map<String, Object> invoke(){
        return invoke(apsKpiTemplate);
    }

    private Map<String, Object> invoke(List<ApsKpiTemplate> template){
        if(apsData==null){
            throw new NullPointerException("未配置算法生成数据");
        }
        if(excutor == null){
            throw new NullPointerException("未配置 KPI 执行器");
        }
        if(template == null){
            template = loadTemplate();
        }
        try{
            kpiResultMap = excutor.applay(apsData,template);
        }
        catch (Exception e){
            log.error("执行 KPI 计算失败", e);
            throw e;
        }
        return kpiResultMap;
    }




    public List<ApsKpiTemplate> loadTemplate() {
//        ApsKpiTemplateMapper datasource = CoreApplicationContext.getApplicationContext().getBean(ApsKpiTemplateMapper.class);
//        //先写死一个 tenantid
//        String tenantId = "1";
//        LambdaQueryWrapper<ApsKpiTemplate> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ApsKpiTemplate::getTenantId, tenantId);
//        List<ApsKpiTemplate> apsKpiTemplates = datasource.selectList(queryWrapper);
//        if(CollectionUtils.isEmpty(apsKpiTemplates)){
//            throw new NullPointerException("tenant：" + tenantId +"未配置kpi行业模板");
//        }
//        return apsKpiTemplates.get(0);
        return null; //todo 加表结构
    }

    public void setApsKpiTemplate(List<ApsKpiTemplate> apsKpiTemplate) {
        this.apsKpiTemplate = apsKpiTemplate;
    }

    public void setApsData(T apsData) {
        this.apsData = apsData;
    }

    public void setExcutor(KpiTemplateExcutor<T> excutor) {
        this.excutor = excutor;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        apsKpiTemplate = loadTemplate();
    }
}
